/** 
 * @(#)IdCardUtil.java 1.0.0 2015年4月16日 上午11:14:25  
 *  
 * Copyright © 2015 .  All rights reserved.  
 */

package com.niuwa.p2p.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.google.common.base.Strings;
import com.niuwa.p2p.core.utils.DictConstant;

/**
 * 身份证工具类
 * 
 * @author zhangmingtao
 * @version $Revision:1.0.0, $Date: 2015年4月16日 上午11:14:25 $
 */
public class IdCardUtil {

	/**
	 * 返回性别F:女、M：男
	 * 
	 * @param idCard
	 * @return
	 */
	public static String getSex(String idCard) {

		String sGender = "";
		if (idCard.length() == IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			idCard = IdCardVerifyHelper.conver15CardTo18(idCard);
		}
		String sCardNum = idCard.substring(16, 17);
		if (Integer.parseInt(sCardNum) % 2 != 0) {
			sGender = DictConstant.Sex.M.toString();
		} else {
			sGender = DictConstant.Sex.F.toString();
		}
		return sGender;
	}

	/**
	 * 根据身份编号获取生日
	 * 
	 * @param idCard
	 *            身份编号
	 * @return 生日(yyyyMMdd)
	 */
	public static String getBirthByIdCard(String idCard) {
		Integer len = idCard.length();
		if (len < IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			return null;
		} else if (len == IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			idCard = IdCardVerifyHelper.conver15CardTo18(idCard);
		}
		return idCard.substring(6, 14);
	}

	/**
	 * @根据日期以及日期格式，获取生日日期
	 * 
	 * @return String
	 */
	public static Date getDate(String date, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据身份证号，自动获取对应的星座
	 * 
	 * @param idCard
	 *            身份证号码
	 * @return 星座
	 */
	public static String getConstellationById(String idCard) {
		if (!IdCardVerifyHelper.validateCard(idCard)) {
			return "";
		}
		int month = getMonthByIdCard(idCard);
		int day = getDateByIdCard(idCard);

		return getConstellation(month, day);
	}

	/**
	 * 根据生日返回星座
	 * 
	 * @param date
	 *            格式为yyyyMMdd、yyyy-MM-dd
	 * @return
	 */
	public static String getConstellationDate(String dateStr) {
		if (Strings.isNullOrEmpty(dateStr)) {
			return "";
		}
		dateStr = dateStr.replaceAll("-", "").trim();
		if (dateStr.length() != 8) {
			return "";
		}
		return getConstellation(Integer.parseInt(dateStr.substring(4, 6)), Integer.parseInt(dateStr.substring(6, 8)));
	}

	/**
	 * 判断星座
	 * 
	 * @param month
	 * @param day
	 * @return
	 */
	private static String getConstellation(int month, int day) {
		String strValue = "";
		if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
			/* 水瓶座 */
			strValue = DictConstant.CONSTELLATION.AQUARIAN.getCode();
		} else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
			/* 双鱼座 */
			strValue = DictConstant.CONSTELLATION.PISCES.getCode();
		} else if ((month == 3 && day > 20) || (month == 4 && day <= 19)) {
			/* 白羊座 */
			strValue = DictConstant.CONSTELLATION.ARIES.getCode();
		} else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
			/* 金牛座 */
			strValue = DictConstant.CONSTELLATION.TAURUS.getCode();
		} else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
			/* 双子座 */
			strValue = DictConstant.CONSTELLATION.GEMINI.getCode();
		} else if ((month == 6 && day > 21) || (month == 7 && day <= 22)) {
			/* 巨蟹座 */
			strValue = DictConstant.CONSTELLATION.CANCER.getCode();
		} else if ((month == 7 && day > 22) || (month == 8 && day <= 22)) {
			/* 狮子座 */
			strValue = DictConstant.CONSTELLATION.LEONIS.getCode();
		} else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
			/* 处女座 */
			strValue = DictConstant.CONSTELLATION.VIRGO.getCode();
		} else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
			/* 天秤座 */
			strValue = DictConstant.CONSTELLATION.LIBRA.getCode();
		} else if ((month == 10 && day > 23) || (month == 11 && day <= 22)) {
			/* 天蝎座 */
			strValue = DictConstant.CONSTELLATION.SCORPIO.getCode();
		} else if ((month == 11 && day > 22) || (month == 12 && day <= 21)) {
			/* 射手座 */
			strValue = DictConstant.CONSTELLATION.SAGITTARIUS.getCode();
		} else if ((month == 12 && day > 21) || (month == 1 && day <= 19)) {
			/* 魔羯座 */
			strValue = DictConstant.CONSTELLATION.CAPRICORNUS.getCode();
		}
		return strValue;
	}

	/**
	 * 根据身份证号码获得年龄
	 * 
	 * @param idCard
	 * @return
	 * @throws Exception
	 */
	public static Short getAge(String idCard){
		Date birthDay = getDate(getBirthByIdCard(idCard), "yyyyMMdd");
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDay)) {
			return null;
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}
		return age <= 0 ? null : new Short(String.valueOf(age));
	}

	/**
	 * 根据身份编号获取生日年
	 * 
	 * @param idCard
	 *            身份编号
	 * @return 生日(yyyy)
	 */
	public static Short getYearByIdCard(String idCard) {
		Integer len = idCard.length();
		if (len < IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			return null;
		} else if (len == IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			idCard = IdCardVerifyHelper.conver15CardTo18(idCard);
		}
		return Short.valueOf(idCard.substring(6, 10));
	}

	/**
	 * 根据身份编号获取生日月
	 * 
	 * @param idCard
	 *            身份编号
	 * @return 生日(MM)
	 */
	public static Short getMonthByIdCard(String idCard) {
		Integer len = idCard.length();
		if (len < IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			return null;
		} else if (len == IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			idCard = IdCardVerifyHelper.conver15CardTo18(idCard);
		}
		return Short.valueOf(idCard.substring(10, 12));
	}

	/**
	 * 根据身份编号获取生日天
	 * 
	 * @param idCard
	 *            身份编号
	 * @return 生日(dd)
	 */
	public static Short getDateByIdCard(String idCard) {
		Integer len = idCard.length();
		if (len < IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			return null;
		} else if (len == IdCardVerifyHelper.CHINA_ID_MIN_LENGTH) {
			idCard = IdCardVerifyHelper.conver15CardTo18(idCard);
		}
		return Short.valueOf(idCard.substring(12, 14));
	}

	public static void main(String[] args) {
		String idCard = "31012520770428062";
		System.out.println(getDateByIdCard(idCard));
		System.out.println(getMonthByIdCard(idCard));
		System.out.println(getBirthByIdCard(idCard));
		System.out.println(getConstellationById(idCard));
		System.out.println(getConstellationDate("2010-01-26"));
		try {
			System.out.println(getAge(idCard));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
